package com.feng.spring.hibernate.extend.util;

import com.feng.spring.hibernate.extend.conf.HibernateExtendConfiguration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class LocationUtil {

    public static String getLocations(Environment environment) {

        Set<String> locationSet = new HashSet<>();
        String locations = environment.getProperty(HibernateExtendConfiguration.LOCATION_NAME);
        if (StringUtils.hasText(locations)) {
            locationSet.addAll(Arrays.stream(locations.split(",|;")).filter(StringUtils::hasText)
                    .map(environment::resolvePlaceholders).collect(Collectors.toList()));
        }
        if (locationSet.isEmpty()) {
            locationSet.add("classpath*:/**/*_*.sql");
        }
        return StringUtils.collectionToCommaDelimitedString(locationSet);
    }
}
